﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Net;

namespace WFAExamAuto
{
    public partial class FrmLogin : Form
    {
     
        public FrmMain fmain;
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            btnLogin.Focus();
            if (fmain.tsbtnLogin.Text == "系统登录")
            {
                   if (!fmain.Ns.IsConnected())
                   {
                       if (!fmain.Ns.Connect(Properties.Settings.Default.ServerIP,Properties.Settings.Default.ServerPort))
                       {
                           if (MessageBox.Show(this, "连接服务器失败，请稍后重试！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK)
                           {
                               return;
                           }
                           return;
                       }
                   }
                string s = fmain.Ns.Login(txtboxName.Text, txtboxPassword.Text);
                if (s == null)
                    MessageBox.Show("登录超时");
                else
                {
                    try
                    {
                        int a = Convert.ToInt32(s);
                        MessageBox.Show("欢迎你：" + txtboxName.Text);
                        fmain.UserName = txtboxName.Text;
                        Properties.Settings.Default.UserName = txtboxName.Text;
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show(s);
                    }
                }
            
                /*if (Ns.IsNeedUpdate(1))
                {
                    this.Close();
                    return;
                }*/
            }
            else
            {
                fmain.tsbtnLogin.Text = "系统登录";
            }

        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            
           // if (Connection.config["LoginName"] != null) txtboxName.Text = "qiuxing"; //Connection.config["LoginName"];
            //if (Connection.config["LoginPwd"] != null) txtboxPassword.Text = Connection.config["LoginPwd"];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtboxName.Text = "qiuxing";
            txtboxPassword.Text = "123";
            btnLogin_Click(sender,e);
        }
 
    }
}
